﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Common.EasyUICommon;
using Simple.ViewModel;

namespace Simple.Web.Framework.Infrastructure.Aop
{
    public class SafeCheckFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //有值就做检查
            if (!string.IsNullOrEmpty(WebSiteConfig.SafeIpAddress))
            {
                string[] safeIpaddressList = WebSiteConfig.SafeIpAddress.Split(';');
                string requestIp = filterContext.HttpContext.Request.UserHostAddress;
                if (!safeIpaddressList.Contains(requestIp))
                {
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        filterContext.Result = new JsonNetResult()
                        {
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                            Data = SimpleResult.GetError("非法访问")
                        };
                    }
                    else
                    {
                        filterContext.Result = new ContentResult() { Content = "非法访问" };
                    }
                }
            }
        }

    }
}